package com.qubuyer.business.auth.view

import android.os.Bundle
import android.view.View
import androidx.core.widget.doAfterTextChanged
import com.qubuyer.R
import com.qubuyer.business.auth.viewmodel.UpdatePhoneViewModel
import com.qubuyer.core.app.MVVMFragment
import com.qubuyer.databinding.FragmentUpdatePhoneStepFirstBinding
import com.qubuyer.databinding.FragmentUpdatePhoneStepSecondBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

/**
 * Author: linwg
 * Time: 2021/3/11 0011
 * Description:
 */
class UpdatePhoneStepSecondFragment : MVVMFragment<FragmentUpdatePhoneStepSecondBinding, UpdatePhoneViewModel>() {

    override fun onDataBinding(dataBinding: FragmentUpdatePhoneStepSecondBinding) {
        dataBinding.host = this
    }

    override val viewModel: UpdatePhoneViewModel by injectVMByActivity()

    override val layoutId: Int
        get() = R.layout.fragment_update_phone_step_second

    private var inCountDown = false

    override fun getTitle(): String {
        return "更换手机"
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewDataBinding.etMobile.doAfterTextChanged {
            viewDataBinding.tvGetCode.isEnabled = it?.length == 11
            viewDataBinding.tvSure.isEnabled = it?.length == 11 && viewDataBinding.etCode.length() == 6
        }
        viewDataBinding.etCode.doAfterTextChanged {
            viewDataBinding.tvSure.isEnabled = it?.length == 6 && viewDataBinding.etMobile.length() == 11
        }
        viewModel.getNewMobileCodeResult.value = false
        viewModel.result.value = 0
        viewModel.getNewMobileCodeResult.observe(viewLifecycleOwner, {
            if (it) {
                toast("验证码获取成功")
                viewDataBinding.tvGetCode.isEnabled = false
                launch {
                    (60 downTo 0).asFlow().onEach { index ->
                        inCountDown = true
                        viewDataBinding.tvGetCode.text = "重新获取${index}S"
                        delay(1000)
                    }.collectLatest {
                        inCountDown = false
                        viewDataBinding.tvGetCode.text = "获取验证码"
                        viewDataBinding.tvGetCode.isEnabled = false
                    }
                }
            }
        })
        viewModel.result.observe(viewLifecycleOwner, {
            when (it) {
                2000 -> {
                    hideKeyBord()
                    mContext.swapContentFragment(UpdatePhoneSuccessFragment())
                }
            }
        })
    }

    fun onClick(v: View) {
        when (v.id) {
            R.id.tvSure -> {
                viewModel.verifyNewMobileSmsCode(viewDataBinding.etMobile.text.toString(), viewDataBinding.etCode.text.toString())
            }
            R.id.tvGetCode -> viewModel.getNewMobileSmsCode(viewDataBinding.etMobile.text.toString())
        }
    }
}